<cfsetting enablecfoutputonly="true" />
<!--- 
|| LEGAL ||
$Copyright: Daemon Pty Limited 1995-2006, http://www.daemon.com.au $
$License: Released Under the "Common Public License 1.0", http://www.opensource.org/licenses/cpl.php$ 

|| VERSION CONTROL ||
$Header: $
$Author: $
$Date: $
$Name: $
$Revision: $

|| DESCRIPTION || 
$Description: Installation scripts for FarCry database components $

|| DEVELOPER ||
$Developer: Brendan Sisson (brendan@daemon.com.au)$
$Developer: Peter Alexandrou (suspiria@daemon.com.au)$
--->

<!--- STEP 1 : setup farcry database --->
<cfparam name="successMsg" default="<td><span class=""success"">DONE</span></td></tr>#chr(13)##chr(10)#" />
<cfparam name="failureMsg" default="<td><span class=""failure"">FAILED!</span></td></tr>#chr(13)##chr(10)#" />


<!--- environment variables --->
<cfparam name="form.sitename" type="string" />
<cfparam name="form.chkPlugins" default="" type="string" />

<!----------------------------------------------------------------------- 
DEPLOY SYSTEM TABLES
	- fqaudit
	- nested_tree_objects
	
------------------------------------------------------------------------>

<!--- build standard schema argument collection --->
<cfset stargs=structnew() />
<cfset stargs.dsn=application.dsn />
<cfset stargs.dbtype=application.dbtype />
<cfset stargs.dbowner=application.dbowner />

<!--- instantiate COAPI utils --->
<cfset oCOAPI=createobject("component", "farcry.core.packages.coapi.coapiadmin").init() />


<cfoutput><h3>System Tables</h3></cfoutput>

<!--- // create fqaudit table --->
<cfoutput><p>Creating audit table.</p></cfoutput>
<cfset fqaudit = createObject("component", "farcry.core.packages.schema.fqaudit").init(argumentcollection=stargs) />
<cfset stResult = fqaudit.createTable() />
<cfflush />

<!--- // create nested_tree_objects table --->
<cfoutput><p>Creating nested tree model table.</p></cfoutput>
<cfset nto = createObject("component", "farcry.core.packages.schema.nested_tree_objects").init(argumentcollection=stargs) />
<cfset stResult = nto.createTable() />
<cfflush />

<!--- // setup refObjects table --->	
<cfoutput><p>Creating refObjects table.</p></cfoutput>
<cfset refobj = createObject("component", "farcry.core.packages.schema.refobjects").init(argumentcollection=stargs) />
<cfset stResult = refobj.createTable() />
<cfflush />

<!--- // set up refContainers table --->
<cfoutput><p>Creating refContainers table.</p></cfoutput>
<!--- 
<cfset nto = createObject("component", "farcry.core.packages.schema.nested_tree_objects").init(argumentcollection=stargs) />
<cfset stResult = nto.createTable() />
 --->
<cfset oCon = createObject("component","#application.packagepath#.rules.container") />
<cfset oCon.deployRefContainers(dsn=application.dsn,dbtype=application.dbtype,dbowner=application.dbowner) />
<cfflush />

<!--- // setup metadata categories --->
<!--- todo: build relevant schema component --->
<cfoutput><p>Creating tables for categorisation actions</p></cfoutput>
<cfset category = createObject("component", "#application.packagepath#.farcry.category") />
<cfset stResult = category.deployCategories(dsn=application.dsn,bDropTables=true) />
<cfflush />

<!--- // setup stats table --->
<!--- todo: build relevant schema component --->
<cfoutput><p>Creating table for site statistics.</p></cfoutput>
<cfset stats = createObject("component", "#application.packagepath#.farcry.stats") />
<cfset stResult = stats.deploy(dsn=application.dsn,bDropTable=true) />
<cfflush />

<!--- // setup config table --->
<!--- todo: build relevant schema component --->
<cfoutput><p>Creating config tables to manage configuration files.</p></cfoutput>
<cfscript>
	o_config = createObject("component", "#application.packagepath#.farcry.config");
	stResult = o_config.deployConfig(dsn=application.dsn,bDropTable=true);
	o_config.defaultVerity(dsn=application.dsn);
	o_config.defaultFile(dsn=application.dsn);
	o_config.defaultImage(dsn=application.dsn);
	o_config.defaultSoEditor(dsn=application.dsn);
	o_config.defaultSoEditorPro(dsn=application.dsn);
	o_config.defaultEWebEditPro(dsn=application.dsn);
	o_config.defaultEOPro(dsn=application.dsn);
	o_config.defaultGeneral(dsn=application.dsn);
	o_config.defaultPlugins(dsn=application.dsn);
	o_config.defaultFU(dsn=application.dsn);
	o_config.defaultOverviewTree(dsn=application.dsn);
	o_config.defaultHTMLArea(dsn=application.dsn);
	o_config.defaultTinyMCE(dsn=application.dsn);
	// bowden 7/23/2006. Added. implied by b301.cfm
	o_config.defaultFCKEditor(dsn=application.dsn);
	// end of add
</cfscript>
<cfflush />

<!--- // deploy rule tables --->
<!--- 
TODO:
	Appear to be missing the container table deploy with the new aggregated rule deploy
	May need to be built as an independent ./schema component
 --->	
<cfset qRules=oCOAPI.getCOAPIComponents(project=form.sitename, package="rules", plugins=form.chkPlugins) />

<cfoutput><p>Creating container and rule tables.</p></cfoutput>
<cfoutput><ul></cfoutput>
<cfloop query="qRules">
	<cfset oRule = createObject("component", qrules.typepath) />
	<cftry>
		<cfset stResult = oRule.deployType(dsn=application.dsn,bDropTable=true,bTestRun=false) />
		<cfoutput><li>#listfirst(qrules.name,".")#</li></cfoutput>
		<cfflush />
	
		<cfcatch type="farcry.core.packages.fourq.tablemetadata.abstractTypeException">
			<cfset stResult.bsucess="false" />
			<cfset stResult.message=cfcatch.message />
		</cfcatch>
	</cftry>
</cfloop>
<cfoutput></ul></cfoutput>


<!--- // deploy type tables --->
<cfset qTypes=oCOAPI.getCOAPIComponents(project=form.sitename, package="types", plugins=form.chkPlugins) />

<cfoutput><p>Creating types tables.</p></cfoutput>
<cfoutput><ul></cfoutput>
<cfloop query="qTypes">
	<cfset oType = createObject("component", qtypes.typepath) />
	<cftry>
		<cfset stResult = oType.deployType(dsn=application.dsn,bDropTable=true,bTestRun=false,dbtype=application.dbtype) />
		<cfoutput><li>#listfirst(qtypes.name,".")#</li></cfoutput>
		<cfflush />
	
		<cfcatch type="farcry.core.packages.fourq.tablemetadata.abstractTypeException">
			<cfset stResult.bsucess="false" />
			<cfset stResult.message=cfcatch.message />
		</cfcatch>
	</cftry>
</cfloop>
<cfoutput></ul></cfoutput>

<!--- 
TODO: 
	am up to here for first pass refactoring
	GB 20061022
 --->



	
	<!--- STEP 3 : setup Daemon security --->
	<cfoutput>
	<table border="0" cellpadding="1" cellspacing="0" width="600">
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr><td colspan="2"><h4>[STEP 3] setup Daemon security (dmSec)</h4></td></tr>
		<cfif browser eq "NS">
			<tr><td colspan="2">&nbsp;</td></tr>
		</cfif>
		<tr><td width="100%"><ul><li>Creating Daemon security tables
	</cfoutput>
	
	<cfscript>dotAnim();</cfscript>
	<cfsilent><cfscript>application.o_dmSecInit.initAuthenticationDatabase(datasource=application.dsn,bTest=false,bDropTables=true);</cfscript></cfsilent>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	<cfoutput><tr><td width="100%"><ul><li>Creating Daemon PolicyStore tables</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfsilent><cfscript>application.o_dmSecInit.initAuthorisationDatabase(datasource=application.dsn,bTest=false,bDropTables=true);</cfscript></cfsilent>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	<cfoutput><tr><td width="100%"><ul><li>Setting up default Policy Groups</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfsilent><cfscript>application.o_dmSecInit.initPolicyGroupsDatabase(datasource=application.dsn,bClearTable=true,core=application.path.core,project=application.path.project,securitypackagepath="#application.packagepath#.security", policygroupimport=expandpath("dmSec_files/policyGroups.wddx"));</cfscript></cfsilent>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	<cfoutput><tr><td width="100%"><ul><li>Setting up default Permissions</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfsilent>
	<cfscript>application.o_dmSecInit.initPermissionsDatabase(datasource=application.dsn,bClearTable=true,core=application.path.core,project=application.path.project,securitypackagepath="#application.packagepath#.security", permissionsimport=expandpath("dmSec_files/permissions.wddx"));</cfscript>
	</cfsilent>
	<cfinclude template="/farcry/core/admin/security/BaseInitialise.cfm">
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	<cfoutput><tr><td width="100%"><ul><li>Creating <strong>farcry</strong> user account</cfoutput>
	<cfscript>
	dotAnim();
	// default groups
	stResult = application.o_dmAuthentication.createGroup(groupName="Contributors",userDirectory="ClientUD",groupNotes="Contributors");
	stResult = application.o_dmAuthentication.createGroup(groupName="Member",userDirectory="ClientUD",groupNotes="Member");
	stResult = application.o_dmAuthentication.createGroup(groupName="Publishers",userDirectory="ClientUD",groupNotes="Publishers");
	stResult = application.o_dmAuthentication.createGroup(groupName="News Contributor",userDirectory="ClientUD",groupNotes="News Contributor");
	stResult = application.o_dmAuthentication.createGroup(groupName="SiteAdmin",userDirectory="ClientUD",groupNotes="Site Administrators");
	stResult = application.o_dmAuthentication.createGroup(groupName="SysAdmin",userDirectory="ClientUD",groupNotes="Systems Administrators");
	// create admin user
	stResult = application.o_dmAuthentication.createUser(userLogin="farcry",userDirectory="ClientUD",userStatus="4",userNotes="Systems Administrator",userPassword="farcry");
	// create profile for admin user
	stProps = structNew();
	stProps.userLogin = "farcry";
	stProps.userDirectory = "ClientUD";
	o_profile = createObject("component", "#application.packagepath#.types.dmProfile");
	stNewProfile = o_profile.createProfile(stProperties=stProps);
	
	// add admin user to SysAdmin group
	stResult = application.o_dmAuthentication.addUserToGroup(userLogin="farcry",groupName="SysAdmin",userDirectory="ClientUD");
	</cfscript>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	<cfoutput><tr><td width="100%"><ul><li>Mapping Policy Groups</cfoutput>
	<cfscript>
	dotAnim();
	// create policy group mappings
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="SysAdmin",userDirectory="ClientUD",policyGroupID="1");
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="SiteAdmin",userDirectory="ClientUD",policyGroupID="2");
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="Contributors",userDirectory="ClientUD",policyGroupID="4");
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="News Contributor",userDirectory="ClientUD",policyGroupID="4");
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="Member",userDirectory="ClientUD",policyGroupID="3");
	stResult = application.o_dmAuthorisation.createPolicyGroupMapping(groupname="Publishers",userDirectory="ClientUD",policyGroupID="5");
	</cfscript>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush /> 

	<!--- load default data into the tree --->
	<cfoutput><tr><td width="100%"><ul><li>Creating Root node in Overview Tree</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfsilent>
	<cfinclude template="_createDefaultNodes.cfm">
	</cfsilent>
	<cfoutput>#successMsg#</cfoutput>
	<cfflush />
	
	<!--- setup default permissions --->
	<cfoutput><tr><td width="100%"><ul><li>Setting up default tree permissions</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfquery name="dPerms" datasource="#application.dsn#">
		delete from #application.dbowner#dmPermissionBarnacle
	</cfquery>
	
	<cffile action="READ" file="#expandpath("dmSec_files/permissionBarnacle.csv")#" variable="permFile" />
	
	<cfloop list="#permFile#" index="lPerms" delimiters="#chr(13)##chr(10)#">
	    <cfscript>
		    oid = listGetAt(lPerms, 3);
		    if (oid neq 'PolicyGroup') lPerms = listSetAt(lPerms, 3, evaluate("#oid#"));
		    application.o_dmAuthorisation.createPermissionBarnacle(reference=listGetAt(lPerms, 3),status=listGetAt(lPerms, 4),policyGroupID=listGetAt(lPerms, 2),permissionID=listGetAt(lPerms, 1));
	    </cfscript>
	</cfloop>
	
	<!--- remove existing permissions cache WDDX file --->
	<cfif fileExists("#application.path.project#/permissionCache.wddx")>
	    <cffile action="DELETE" file="#application.path.project#/permissionCache.wddx">
	</cfif>
	<cfoutput>#successMsg#</cfoutput>
	
	<!--- reset permissions caches --->
	<cfoutput><tr><td width="100%"><ul><li>Resetting permission caches</cfoutput>
	<cfscript>dotAnim();</cfscript>
	<cfset application.o_dmSecInit.initServer(clearExistingCache=true) />
	<cfset application.o_dmSecInit.initSession() />
	<!--- success! --->
	<cfoutput>#successMsg#</cfoutput>

	<cfoutput>
	</table>
	</cfoutput>
	<cfflush />

<!----------------------------------------------------------------------
Plugin
 - search and install Plugin install data
----------------------------------------------------------------------->
<cfset qInstalls=oCOAPI.getPluginInstallers(plugins=form.chkPlugins) />
<cfloop query="qInstalls">
	<cfinclude template="/farcry/plugins/#qinstalls.Plugin#/config/install/#qinstalls.name#" />
</cfloop>

<cfsetting enablecfoutputonly="false" />